import matplotlib.pyplot as plt
import numpy as np
import statistics
import matplotlib.style
import matplotlib as mpl
mpl.style.use('classic')

plt.rcParams.update({'font.size': 20})

def step_(ax, data, label):
    ax.step(np.sort(data), np.arange(1, len(data)+1) / float(len(data)), where='post', label=label, linewidth=3)

def cdf():
    ts = []
    for line in open('inc_fattree.txt'):
        if 'total' in line:
            continue
        arr = line.split(' ')
        ts.append(int(arr[1])/1000)
    print(statistics.mean(ts))
    print(statistics.quantiles(ts, n=10)[8])

    nsw = [112 * 48, 4 * 112, 4 * 48]
    # nsw = [48*24, 48 * 24, 576]
    ST = 21000
    ts1 = []
    nt = []
    for line in open('lec_fattree.txt'):
        if 'total' in line:
            continue
        arr = line.split(' ')
        ts1.append(int(arr[0])/1000)

    for i in range(nsw[0]):
        nt.append(ts1[i % len(ts1)] * 2)
    for i in range(nsw[1]):
        nt.append(ts1[i % len(ts1)] * 2)
    for i in range(nsw[2]):
        nt.append(ts1[i % len(ts1)] * 2 + ST * 2)

    ts1 = nt

    print(statistics.mean(ts1))
    print(statistics.quantiles(ts1, n=10)[8])

    fig, ax = plt.subplots(figsize=(6, 3.5))
    step_(ax, ts, 'APKeep')
    step_(ax, ts1, 'DDPV')
    ax.set_xlabel('Time (us)')
    ax.grid()
    ax.legend(loc='lower right', fontsize='small')
    ax.set_xscale('log')
    # plt.show()
    plt.savefig('inc_ft.png',bbox_inches='tight')

def cdf1():
    ts = []
    for line in open('inc_st.txt'):
        if 'total' in line:
            continue
        arr = line.split(' ')
        ts.append(int(arr[1])/1000)
    print(statistics.mean(ts))
    print(statistics.quantiles(ts, n=10)[8])

    ts1 = []
    for line in open('st_incremental_time.txt'):
        if 'total' in line:
            continue
        arr = line.split(' ')
        ts1.append(int(arr[0])/1000)

    print(statistics.mean(ts1))
    print(statistics.quantiles(ts1, n=10)[8])

    fig, ax = plt.subplots(figsize=(6, 3.5))
    ax.set_xlabel('Time (us)')
    step_(ax, ts, 'APKeep')
    step_(ax, ts1, 'DDPV')
    ax.grid()
    ax.legend(loc='lower right', fontsize='small')
    ax.set_xscale('log')
    # plt.show()
    plt.savefig('inc_st.png',bbox_inches='tight')

cdf()

# + label
# fig tick biger